﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using CarFrameWork.Common;

namespace CarFrameWork.UI.ProductDesign._3DModelingDesign.FenDongQi
{
    public partial class 轴承座螺栓 : PartFormBase
    {
        CarFrameWork.Model.CanShuHua.轴承座螺栓 zczls = new CarFrameWork.Model.CanShuHua.轴承座螺栓();

        public 轴承座螺栓()
        {

            InitializeComponent();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Global.SelectedID = string.Empty;///改动
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!Panduan())
            {
                return;
            }

            if (zczls.Exists(Convert.ToInt32(this.cmbIDList.Text.Trim())) == true)
            {
                FormToModel();
                zczls.ID = Convert.ToInt32(this.cmbIDList.Text.Trim());
                zczls.Update();
                MessageBox.Show("更新成功", "提示");
                Global.SelectedID = this.cmbIDList.Text.Trim();
                this.Close();
            }
            else
            {
                if (TestContent())
                {
                    FormToModel();
                    zczls.Add();
                    MessageBox.Show("添加成功", "提示");
                    Global.SelectedID = this.cmbIDList.Text.Trim();
                    this.Close();
                }
                else
                    MessageBox.Show("参数不能为空！", "提示");
               
            }
        }

        private void FormToModel()
        {
            //tyfcz.ID = 180310350;
            zczls.path = "path";
            zczls.name = "轴承座螺栓";
            zczls.D1 = Convert.ToSingle(textBox1.Text);
            zczls.D3 = Convert.ToSingle(textBox2.Text);
            zczls.D4 = Convert.ToSingle(textBox3.Text);
            zczls.L1 = Convert.ToSingle(textBox4.Text);
            zczls.L3 = Convert.ToSingle(textBox5.Text);
            zczls.L4 = Convert.ToSingle(textBox6.Text);
          
        }

        private bool Panduan()
        {
            for (int i = 3; i < 9; i++)
            {
                TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                if (float.Parse(textbox.Text) <= 0)
                {
                    MessageBox.Show("对话框中的数值不能为零或负数!");
                    return false;
                }
            }

            if (float.Parse(textBox3.Text) < float.Parse(textBox2.Text))
            {
                MessageBox.Show("对话框中的D4<D3,数值不正确!");
                return false;
            }
            if (float.Parse(textBox1.Text) < float.Parse(textBox3.Text))
            {
                MessageBox.Show("对话框中的D1<D4,数值不正确!");
                return false;
            }
            if (float.Parse(textBox5.Text) < float.Parse(textBox4.Text))
            {
                MessageBox.Show("对话框中的L3<L1,数值不正确!");
                return false;
            }
            if (float.Parse(textBox6.Text) < float.Parse(textBox5.Text))
            {
                MessageBox.Show("对话框中的L4<L3,数值不正确!");
                return false;
            }
            return true;

        }

        private bool TestContent()
        {
            if (!InspectionText(this.textBox1))
                return false;
            if (!InspectionText(this.textBox2))
                return false;
            if (!InspectionText(this.textBox3))
                return false;
            if (!InspectionText(this.textBox4))
                return false;
            if (!InspectionText(this.textBox5))
                return false;
            if (!InspectionText(this.textBox6))
                return false;

            return true;
        }

        private void 轴承座螺栓_Load(object sender, EventArgs e)
        {
            foreach (DataRow row in GetIDList(string.Empty).Tables[0].Rows)
            {
                this.cmbIDList.Items.Add(row[0].ToString().Trim());
            }

            bool tag = false;
            foreach (string str in this.cmbIDList.Items)
            {
                if (str == this.m_ID.ToString())
                {
                    tag = true;
                    break;
                }
            }
            if (!tag)
                this.cmbIDList.Items.Add(this.m_ID.ToString().Trim());
            this.cmbIDList.SelectedIndex = 0;
            this.cmbIDList.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private DataSet GetIDList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID ");
            strSql.Append(" FROM [轴承座螺栓] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private DataSet GetDataList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [轴承座螺栓] ");
            if (strWhere.Trim() != string.Empty)
            {
                if (strWhere != "ID = System.Data.DataRowView")
                    strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        private void cmbIDList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ModelToForm(GetDataList("ID = " + cmbIDList.Text.Trim()));
        }

        private void ModelToForm(DataSet dt)
        {
            if (!zczls.Exists(Convert.ToInt32(cmbIDList.Text.Trim())))
            {
                for (int i = 3; i < 9; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = "";
                }
            }
            else
            {
                for (int i = 3; i < 9; i++)
                {
                    TextBox textbox = (TextBox)groupBox1.Controls.Find("textbox" + Convert.ToString(i - 2), true)[0];
                    textbox.Text = dt.Tables[0].Rows[0][i].ToString().Trim();
                }
            }
        }
    }
}